package tgc.edu.library.web.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.library.custom.EasyuiResult;
import tgc.edu.library.custom.LogUtils;
import tgc.edu.library.entity.BorrowRecord;
import tgc.edu.library.entity.Reader;
import tgc.edu.library.entity.SysRole;
import tgc.edu.library.entity.SysUser;
import tgc.edu.library.repository.SysRoleRepository;
import tgc.edu.library.security.UserUtils;
import tgc.edu.library.service.BorrowRecordService;
import tgc.edu.library.service.ReaderService;
import tgc.edu.library.service.SysUserService;
import tgc.edu.library.web.form.ReaderForm;
import tgc.edu.library.web.form.SysUserForm;


@Controller
public class LoginController {
	@Autowired
	private LogUtils logUtils;
	@Autowired
	private UserUtils userUtils;
	@Autowired
	private ReaderService readerService;
	@Autowired
	private SysRoleRepository roleDao;
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private BorrowRecordService borrowService;
	
	@RequestMapping(value= {"/login", "/"})
	public String login(SysUserForm form,ModelMap map) {
		return "login";
	}
	
	@RequestMapping(value="/welcome")
	public String welcome(){
		return "welcome";
	}
	
	@RequestMapping(value = "/main")
	public String main(ModelMap map) {
		SysUser user = userUtils.getUser();
		if(user!=null) {
			logUtils.log(user.getUsername(), "登陆");
			List<BorrowRecord> list = borrowService.findAll();
			List<BorrowRecord> borrow = new ArrayList<>();
			List<BorrowRecord> borrow1 = new ArrayList<>();
			for (BorrowRecord borrowRecord : list) {
				if (borrowRecord.getStatus().equals("申请中")) {
					borrow.add(borrowRecord);
				}else if(borrowRecord.getStatus().equals("已同意")) {
					borrow1.add(borrowRecord);
				}
			}
			map.put("borrow", borrow.size());
			map.put("borrow1", borrow1.size());
			return "main";
		} 
		return "redirect:login";
	}
	
	@RequestMapping("/logout2")
	public String logout2() {
		SysUser user = userUtils.getUser();
		logUtils.log(user.getUsername(), "退出");
		return "redirect:logout";
	}
	
	@RequestMapping("/logout")
	public String logout() {
		return "redirect:logout";
	}
	
	@RequestMapping("/register")
	@ResponseBody
	public Object register(ReaderForm readerForm) {
		Reader reader = new Reader();
		SysRole role = roleDao.findByCode("ROLE_READER");
		if(readerService.findByPhone(readerForm.getPhone())!=null || 
		readerService.findByEmail(readerForm.getEmail())!=null || 
		readerService.findByUsername(readerForm.getUsername())!=null) {
			return new EasyuiResult(false,"该账号/手机号/电子邮箱已被注册！");
		}else {
			reader.setUsername(readerForm.getUsername());
			reader.setPassword(new BCryptPasswordEncoder().encode(readerForm.getPassword()));
			reader.setName(readerForm.getName());
			reader.setEmail(readerForm.getEmail());
			reader.setPhone(readerForm.getPhone());
			reader.getRole().add(role);
			readerService.save(reader);
			return new EasyuiResult("注册成功！");
		}
	}
	
	/**
	 * 判断原密码是否正确
	 * */
	@RequestMapping(value="/password")
	@ResponseBody
	public Boolean pwd(String password) {
		SysUser user = userUtils.getUser();
		BCryptPasswordEncoder encoder=new BCryptPasswordEncoder();
		boolean f = encoder.matches(password,user.getPassword());
		return f;
	}
	
	/****
	 * 修改密码
	 * @param password
	 * @return
	 */
	@RequestMapping(value="/changePwd1")
	@ResponseBody
	public Boolean changePwd1(String password) {
		try {
			SysUser user = userUtils.getUser();
			BCryptPasswordEncoder encoder=new BCryptPasswordEncoder();
			user.setPassword(encoder.encode(password));
			sysUserService.save(user);
			boolean f = true;
			return f;
		} catch (Exception e) {
			boolean f = false;
			return f;
		}
		
	}
}
